<?php

/**
 * This is the model class for table "tbl_course".
 *
 * The followings are the available columns in table 'tbl_course':
 * @property integer $course_id
 * @property integer $user_id
 * @property string $teacher_info
 * @property string $brief
 * @property string $course_name
 * @property string $description
 * @property string $create_date
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property TblUser $user
 */
class Course extends CActiveRecord
{
    const STATUS_ACTIVE = 1;
    const STATUS_DEACTIVE = 0;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Course the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_course';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, course_name, description', 'required'),
			array('user_id, status', 'numerical', 'integerOnly'=>true),
			array('course_name', 'length', 'max'=>100),
			array('teacher_info, brief', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('course_id, user_id, teacher_info, brief, course_name, description, create_date, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                                                    'user' => array(self::BELONGS_TO, 'User', 'user_id'),
                                                    'resources' => array(self::MANY_MANY, 'Resources', 'tbl_course_resources(course_id, resources_id)'),
                                                    'teachers' => array(self::MANY_MANY, 'Teacher', 'tbl_course_teacher(course_id, teacher_id)', 'condition'=>'status=1'),
                                                    'count' => array(self::STAT, 'Teacher', 'tbl_course_teacher(course_id, teacher_id)', 'condition'=>'status=1'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'course_id' => '实验课程ID',
			'user_id' => '任课教师',
			'teacher_info' => '任课教师简介',
			'brief' => '实验课程简介',
			'course_name' => '实验课程名称',
			'description' => '实验课程内容',
			'create_date' => '创建时间',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('course_id',$this->course_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('teacher_info',$this->teacher_info,true);
		$criteria->compare('brief',$this->brief,true);
		$criteria->compare('course_name',$this->course_name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
